import axios from "axios"
import Vue from 'vue'

function getLanguage() {
  if (!sessionStorage.langSort) {
    axios({
      baseURL: process.env.NODE_ENV == "development" ? 'http://192.168.1.190:8012/' : '/',
      url: "./smartEms/LanguageSearchCmd",
      method: "post",
      contentType: "application/text,charset=utf-8",
      data: JSON.stringify({
        requestCommand: "",
        responseCommand: ""
      }),
      dataType: "json"
    }).then(res => {
      let data = res.data;
      sessionStorage.langSort = data.requestCommand;
      setLanguage()
    })
  } else {
    setLanguage()
  }
}

function setLanguage() {
  var langSort = sessionStorage.langSort ? sessionStorage.langSort : 'zh';
  axios({
    baseURL: process.env.NODE_ENV == "development" ? '/' : '../emsmain/',
    url: "static/language/language_" + langSort + ".json",
    method: "get",
    contentType: "application/text,charset=utf-8",
    data: {},
    dataType: "json",
  }).then(res => {

    let data = res.data;
    sessionStorage.language = JSON.stringify(data);
    Vue.prototype.$t = function (str) {

      var keyStr = str.split(".")[0];
      var valueStr = str.split(".")[1];
      return data[keyStr][valueStr];
    }


  })
}
export default {
  getLanguage,
  setLanguage
}
